package m.app.action;

import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.store.model.GuestInfo;
import m.store.model.StockInfo;
import m.store.model.StoreGoods;
import m.store.service.GuestInfoService;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "app/guestInfo",title = "app客户信息")
public class GuestInfoAction extends AppAction{
    private GuestInfo model;
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "客户列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[querySelf]",description = "查自己公司",type = FieldType.STRING,length = 50,dictOption = "Y:是,N:否"),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage getList() throws Exception {
        String querySelf=getParamsValue("querySelf");
        ModelQueryList queryList=toQueryList(GuestInfo.class,"*","manageUser.realname");
        queryList.addCondition(QueryCondition.eq("store.oid", PowerUtil.getSessionStoreOid()));
        if(!"Y".equals(querySelf))
            queryList.addCondition(QueryCondition.not(QueryCondition.eq("oid",PowerUtil.getSelfGuestInfo().getOid())));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "查询客户信息",permission = true),
        params = {
            @DocumentParamMeta(name = "model.oid",description = "客户oid",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage getInfo() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","inputUser.realname","manageUser.realname");
            PowerUtil.verifyStoreData(model.getStore().getOid());
        }else{
            if(null==model){
                model=new GuestInfo();
                model.setType(GuestInfo.Type_Company);
            }
            model.setStore(PowerUtil.getSessionStore(true));
            model.setInputUser(PowerUtil.getSessionUser(true));
            model.setManageUser(PowerUtil.getSessionUser());
            model.setStockAmount(0d);
            model.setStockPayAmount(0d);
            model.setSaleAmount(0d);
            model.setSaleIncomeAmount(0d);
            model.setSurplusAmount(0d);
        }
        return JSONMessage.success("查询成功")
                .push("model",model);
    }

    public GuestInfo getModel() {
        return model;
    }

    public void setModel(GuestInfo model) {
        this.model = model;
    }
}
